const app = getApp();

Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    theme: app.globalData.theme, //全局颜色变量
    lat: null,
    lng: null,
    expressSiteList: []
  },
  onLoad() {
    this.loadExpressSite()
  },
  onReady() {},
  loadExpressSite: function () {
    const _this = this;
    app.checkLocation().then(function (res) {
      app.getCurrentLocation().then(function (res) {
        _this.setData({
          lat: res.latitude,
          lng: res.longitude
        });
        _this.loadExpressSiteList();
      }).catch(function (error) {
        console.log(error);
        _this.loading = false;
        wx.showToast({
          title: "获取位置信息失败，请检查是否打开位置权限",
          icon: 'none',
          duration: 2000
        });
        _this.loadExpressSiteList();
      });
    });
  },
  loadExpressSiteList: function () {
    app.api.expressSiteList({
      latitude: this.data.lat || '',
      longitude: this.data.lng || ''
    }).then(res => {
      let expressSiteList = res.data || []
      if (expressSiteList.length > 0) {
        this.setData({
          expressSiteList: expressSiteList
        });
      }
    });
  },
  selectExpressSite: function (e) {
    let index = e.currentTarget.dataset.index
    app.globalData.expressSite = this.data.expressSiteList[index]
    wx.navigateBack()
  }
});